package com.syqc.monitor.ui.carinfo.report

import android.content.Intent
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import autodispose2.autoDispose
import com.kingja.loadsir.core.LoadService
import com.kingja.loadsir.core.LoadSir
import com.syqc.adapter.DayMileAdapter
import com.syqc.adapter.PhotosHistoryAdapter
import com.syqc.comlib.base.LazyFragment
import com.syqc.comlib.callback.EmptyCallback
import com.syqc.monitor.R
import com.syqc.monitor.databinding.FragmentReportItemBinding
import com.syqc.monitor.databinding.IncludeDayChooseBinding
import com.syqc.monitor.databinding.ItemDayMileTitleBinding
import com.syqc.monitor.ui.photo.PhotoDetailActivity
import com.syqc.utils.DataUtil
import com.syqc.utils.Keys
import com.syqc.utils.ToolsUtil
import com.syqc.videos.CarMonitorViewModel

/**
 *@Author Jasper
 *@Time   2020/10/15
 *@Desc   车辆报表
 */
class ReportItemFragment(val type: Int) :
    LazyFragment<FragmentReportItemBinding>(FragmentReportItemBinding::inflate) {
    private lateinit var loadService: LoadService<Any>
    private lateinit var mDayChooseVB: IncludeDayChooseBinding
    private val mActivityVM by activityViewModels<CarMonitorViewModel>()
    private val mViewModel: ReportItemViewModel by viewModels()
    private val dayMileAdapter by lazy { DayMileAdapter() }
    private val photosAdapter by lazy { PhotosHistoryAdapter(requireContext()) }


    override fun initView(view: View) {
        mDayChooseVB = IncludeDayChooseBinding.bind(binding.root)
        mViewModel.dataType = type
        loadService = LoadSir.getDefault().register(binding.includeRecycler.recyclerView)
        mActivityVM.photoUpdate.observe(this) {
            binding.includeRecycler.recyclerView.scrollToPosition(it)
            photosAdapter.index = it
            photosAdapter.notifyDataSetChanged()
        }
        mActivityVM.carName.observe(this) {
            mViewModel.carName = it
        }
        mActivityVM.carId.observe(this) {
            if (it.isNullOrEmpty()) {
                loadService.showCallback(EmptyCallback::class.java)
            } else {
                mViewModel.carId = it
                if (!mViewModel.isHidden) mViewModel.handleTime()
            }
        }
    }

    override fun initData() {
        if (mViewModel.carId.isEmpty()) showToast(R.string.hint_toast_choose_car)
        loadService.showCallback(EmptyCallback::class.java)
        initRecycler()
        handleViewModel()
    }

    private fun handleViewModel() {
        mViewModel.toastObserver().autoDispose(scopeProvider).subscribe(this::toastObserver)

        mViewModel.dayMileSub.hide().autoDispose(scopeProvider).subscribe {
            loadService.showSuccess()
            dayMileAdapter.setList(it.list)
        }
        mViewModel.freeEmptySub.hide().autoDispose(scopeProvider).subscribe {
            if (it) {
                loadService.showCallback(EmptyCallback::class.java)
            } else {
                loadService.showSuccess()
            }
        }
        mViewModel.photosSub.hide().autoDispose(scopeProvider).subscribe {
            photosAdapter.setList(it)
            DataUtil.mPhotoInfo = it
            binding.includeRecycler.recyclerView.scrollToPosition(0)
        }
    }

    override fun initEvent() {
        mDayChooseVB.rdbReportYesterday.setOnClickListener {
            mViewModel.timeType = 1
            mViewModel.handleTime()
        }
        mDayChooseVB.rdbReportToday.setOnClickListener {
            mViewModel.timeType = 2
            mViewModel.handleTime()
        }
        mDayChooseVB.rdbReportThree.setOnClickListener {
            mViewModel.timeType = 3
            mViewModel.handleTime()
        }
        mDayChooseVB.rdbReportWeek.setOnClickListener {
            mViewModel.timeType = 4
            mViewModel.handleTime()
        }
    }

    private fun initRecycler() {
        binding.includeRecycler.recyclerView.layoutManager = LinearLayoutManager(requireContext())
        when (type) {
            0 -> {
                binding.includeRecycler.recyclerView.adapter = dayMileAdapter
                dayMileAdapter.addHeaderView(initHeadView())
            }
            3 -> {
                binding.includeRecycler.recyclerView.adapter = photosAdapter
                photosAdapter.setOnItemClickListener { adapter, view, position ->
                    if (ToolsUtil.isFastClick()) return@setOnItemClickListener
                    mActivityVM.updateLauncher?.launch(
                        Intent(
                            requireContext(),
                            PhotoDetailActivity::class.java
                        ).apply {
                            putExtra(Keys.INTENT_TITLE, mViewModel.carName)
                            putExtra(Keys.INTENT_OTHER, position)
                        })
                }
            }
        }
    }

    fun initHeadView(): View {
        val binding = ItemDayMileTitleBinding.inflate(layoutInflater)
        return binding.root
    }

    //是否隐藏状态变化监听
    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (hidden) {
            mViewModel.isHidden = true
        } else {
            mViewModel.isHidden = false
            if (mViewModel.carId.isNotEmpty()) mViewModel.handleTime()

        }
    }

}